<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form v-if="admShow()" :inline="true" :model="formFilter" ref="formFilter">
          <el-form-item label="企业名称" prop="companyNo">
            <el-select v-model="formFilter.companyNo" @change="selectChange">
              <el-option 
                v-for="item in CompList"
                :key="item.companyNo"
                :label="item.companyName"
                :value="item.companyNo"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form>
      <!-- <vue-header-button>
        <el-button type="info" @click="create">开户</el-button>
      </vue-header-button> -->
    </vue-header>
    <vue-body>
      <el-table
        v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="companyName"
          label="企业名称">
        </el-table-column>
        <el-table-column
          prop="applyDate"
          label="申请时间">
        </el-table-column>
        <el-table-column
          prop="applyUserName"
          label="申请人姓名">
        </el-table-column>
        <el-table-column
          prop="status"
          label="申请状态" width="100">
          <template slot-scope="scope">
            {{ scope.row.applyStatus | applyStatus  }}
          </template>
        </el-table-column>
        <el-table-column v-if="admShow()" label="操作" width="220">
          <template slot-scope="scope">
            <el-button-group>
            <el-button
              size="mini"
              type="info"
              @click="handleDetail(scope.$index, scope.row, tableData)">预览</el-button>
            <el-button
              size="mini"
              v-show = "scope.row.applyStatus === '02'"
              @click="pass(scope.row)">通过</el-button>
            <el-button
              size="mini"
              v-show = "scope.row.applyStatus === '02'"
              @click="fail(scope.row)">不通过</el-button>
              </el-button-group>
          </template>
      </el-table-column>
      </el-table>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <br />
    <div class="applyfooter" v-if="!admShow()" >
      <el-button style="float:left" type="info" @click="back()">后 退</el-button>
    </div>
    <edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='pass' || view ==='fail'"/>
  </vue-content>
</template>

<script>
  import {mapGetters} from 'vuex'
  import {queryCompanyList} from '@/services/staffManager'
  import {query,checkCompany} from '@/services/applyManager'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  export default {
    name: 'comp-table',
    components: {EditRole},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      CompList:[],
      formFilter: {
        companyNo: ""
      },
      loading: false,
      view: ''
    }),
    computed: {
      ...mapGetters(['token'])
    },
    methods: {
      admShow(){
        if(this.token.userType == '01'){
          return true
        }else{
          return false
        }
      },
      async loadCompList () {
        try {
          let data = await queryCompanyList();
          // this.formFilter.companyNo = data.data[0].companyNo;
          // console.log(data)
          this.CompList = data.data;
          this.loadData();
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async loadData () {
        this.loading = true
        try {
          let data = await query(Object.assign({currentPage: this.currentPage},{perPage:10}, this.formFilter))
          // debugger
          this.tableData = data.data
					this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      imgSrc(data){
      	return this.$store.state.imgUrl + data;
      },
      styleStr(data){
      	return "width: 20px;height: 20px;border: 1px solid #aaa;margin-left:10px; border-radius: 5px;background: #"+data+";"
      },
      async pass (data) {
        this.view = 'pass';
        await this.$nextTick();
        this.$refs['edit-role'].formData = deepClone(data)
      },
      async fail (data) {
        this.view = 'fail';
        await this.$nextTick();
        this.$refs['edit-role'].formData = deepClone(data)
      },
      async handleDetail (index, data) {
        // this.view = 'detail'
        // await this.$nextTick()
        // this.$refs['edit-detail'].formData = deepClone(data)
        // this.$refs['edit-detail'].loadData();
        this.$router.push({
	        path: 'companyChangeVerify/preview', 
	        name: 'preview',
	        query: { 
            applyNo: data.applyNo
          }
        })
      },
      back(){
        this.$router.go(-1)
      },
      selectChange(item){
        // console.log(item)
        this.formFilter.companyNo = item;
        this.loadData()
      },
      SearchSubmit () {
        this.currentPage = 1
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      editFinish (needRefresh) {
        this.view = ''
        if(needRefresh){
          this.$nextTick(() => {
            // dom元素更新后执行， 因此此处能正确打印出更改之后的值；
            this.loadData()
         	})
        }
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      applyStatus (value) {
        return {"00": '删除',"01": '草稿', "02": '已提交', "03": '已审核', "04": '拒绝', "05": '已完成'}[value]
      },
      isIntegral(value){
        return {"0": '不允许', "1": '允许'}[value]
      }
    },
    created () {
      this.loadCompList();
    }
  }
</script>

<style>
	.logoImgPic{
		height: 100px;
		display: block;
	}
	.el-table th>.cell{
		text-align: center;
	}
</style>
